/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bezrabotnih.cabinet.client.activity;

import java.util.List;
import java.util.Set;

import net.bezrabotnih.cabinet.client.place.CreateJobPlace;
import net.bezrabotnih.cabinet.client.place.ProviderJobsPlace;
import net.bezrabotnih.cabinet.client.request.CabinetRequestFactory;
import net.bezrabotnih.cabinet.client.ui.CreateEditJobView;
import net.bezrabotnih.cabinet.client.ui.CreateEditJobViewImpl;
import net.bezrabotnih.client.managed.request.JobProxy;
import net.bezrabotnih.client.managed.request.JobRequest;
import net.bezrabotnih.client.managed.request.JobTypeProxy;
import net.bezrabotnih.client.managed.request.UserAccountProxy;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.Violation;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

/**
 * Activities are started and stopped by an ActivityManager associated with a container Widget.
 */
public class CreateJobActivity extends AbstractActivity implements CreateEditJobView.Presenter {

	/**
	 * Sample property.
	 */
	private CreateJobPlace place;
	private CabinetRequestFactory requestFactory;
	private PlaceController placeController;
	
	private CreateEditJobView view;

	public CreateJobActivity(CreateJobPlace place,CabinetRequestFactory requestFactory,PlaceController placeController) {
	    this.place=place;
	    this.requestFactory=requestFactory;
	    this.placeController=placeController;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = CreateEditJobViewImpl.instance();
		view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
		view.setEditView(false);
		if (!view.isAllJobTypesListInited()){
			requestFactory.cabinetJobTypeRequest().findAllJobTypes().fire(new Receiver<List<JobTypeProxy>>() {
				@Override
				public void onSuccess(List<JobTypeProxy> jobTypeProxies) {
					view.initAllJobTypesList(jobTypeProxies);
				}
			});
			
		}else{
			view.resetForms();
		}
		
		
	}
	

	@Override
	public String mayStop() {
		return null;
	}

	/**
	 * @see CreateEditJobView.Presenter#goTo(Place)
	 */
	public void goTo(Place place) {
		// TODO
	}

	@SuppressWarnings("deprecation")
	@Override
	public void saveClicked() {
		
		
		final JobRequest jobRequest=requestFactory.jobRequest();
		final JobProxy jobProxy=jobRequest.create(JobProxy.class);
		
		jobProxy.setDeadLine(view.getDeadlineDate());
		jobProxy.setDescription(view.getDescription());
		jobProxy.setIsHourly(view.isHourly());
		jobProxy.setJobTypeId(view.getJobTypeId());
		jobProxy.setName(view.getName());
		jobProxy.setPrice(view.getPrice());
		jobProxy.setStartDate(view.getStartDate());
		jobProxy.setStatus(0);
		
		
		requestFactory.cabinetUserAccountRequest().currentUserAccount().fire(new Receiver<UserAccountProxy>() {
			@Override
			public void onSuccess(final UserAccountProxy userAccountProxy) {
				jobProxy.setProviderId(userAccountProxy.getId());
				jobRequest.persist().using(jobProxy).fire(new Receiver<Void>() {
					@Override
					public void onSuccess(Void response) {
						
					placeController.goTo(new ProviderJobsPlace());
					}
					@Override
					public void onViolation(Set<Violation> errors) {
						String errorStr="";
						for (Violation error:errors){
							errorStr+=error.getPath()+" "+error.getMessage()+", ";
						}
						Window.alert(errorStr);
					}
				});
			}
		});
		
		
		
	}
}
